<?php
$flag = 0;
$total_money = 0;
$f = false;
$f1 = 0;
$k = 0;

if (isset($_POST) && $_POST != NULL) {
    $money_user = Frontend::findUserById((int) $_SESSION["user_id"]);
    if (isset($_SESSION["user_id"]) && $_SESSION["user_id"] != "") {
        foreach ($_SESSION["cart"] as $key => $_cart) {
            foreach ($_SESSION["cart"][$key] as $_key => $_cart_detail) {
                $base_decode = base64_decode($_cart_detail);
                $json_decode = json_decode($base_decode, true);

                if ($_today < $json_decode["time"]) {
                    $k = $key . '_' . $_key;
                    $m = $_POST["number_money"]["$k"];
                    if ($m > 0) {
                        if (!$json_decode["money"]) {
                            $json_decode = $json_decode + array("money" => $m);
                            $json_decode = $json_decode + array("index_key" => $_POST["index_key"][$k]);
                        } else {
                            $json_decode["money"] = $m;
                            $json_decode["index_key"] = $_POST["index_key"][$k];
                        }
                        $_SESSION["cart"][$key][$_key] = $json_decode;
                        $json_encode = json_encode($json_decode);
                        $base_encode = base64_encode($json_encode);
                        $_SESSION["cart"][$key][$_key] = $base_encode;
                    } else {
                        $f1 = 3;
                        $f = true;
                        break;
                    }
					
                    if ($json_decode["type"] == 2 && $json_decode["money"] % 20 != 0) {
                        $f1 = 1;
                        $f = true;
                        break;
                    } else {
                        $_SESSION["cart"][$key][$_key] = $json_decode;
                        $total_money = $total_money + $_SESSION["cart"][$key][$_key]["money"];
                        $json_encode = json_encode($json_decode);
                        $base_encode = base64_encode($json_encode);
                        $_SESSION["cart"][$key][$_key] = $base_encode;
                    }
                } else {
                    $f1 = 2;
                    $f = true;
                }
            }
        }
        if (!$f) {
            foreach ($money_user as $_money_user) {
                if ($total_money > (int) $_money_user["gold"]) {
                    $flag = 1;
                    break;
                } else {
                    foreach ($_SESSION["cart"] as $key => $_cart) {
                        foreach ($_SESSION["cart"][$key] as $_key => $_cart_detail) {
                            $base_decode = base64_decode($_cart_detail);
                            $json_decode = json_decode($base_decode, true);
                            try {
                                if ($key == "bongda") {
                                    Sys::addUserOdd($json_decode, $_SESSION["user_id"]);
                                }
                                if ($key == "soxo") {
                                    Sys::addUserOddSx($json_decode, $_SESSION["user_id"]);
                                }
                            } catch (Exception $exc) {
                                echo "Lỗi dự đoán kết quả";
                            }
                        }
                    }
                    $_money_user = $_money_user + array("money" => $total_money);
                    Sys::updateUserMoney($_money_user, $_SESSION["user_id"]);
                    unset($_SESSION["cart"]);
                    $flag = 2;
                }
            }
        }
    } else {
        echo "<script>window.location.href = 'http://dudoanthethao.com.vn/login';</script>";
    }
}
?>
<div id="loaddata" class="span14">
    <form action="" id="verifyForm" class="form-horizontal" method="post">

        <?php
        if (isset($_SESSION["cart"])) {
            ksort($_SESSION["cart"]);
            foreach ($_SESSION["cart"] as $__key => $__cart) {
                ?>
                <legend><?php if ($__key == "bongda") { ?>Dự Đoán Kết Quả Bóng Đá<?php } else { ?>Dự Đoán Kết Quả Xổ Số ( Đề : 1 coins = 1 điểm - Lô 20 coins = 1 điểm )<?php } ?></legend>
                <?php if ($f) { ?>
                    <div id="message" class="alert alert-error" style="<?php
                    if ($flag == 1 || $f1 == 1 || $f1 == 2 || $f1 == 3) {
                        echo "display: block;";
                    } else {
                        echo "display: none;";
                    }
                    ?>"><?php
                             if ($flag == 1) {
                                 echo "Tài khoản bạn không đủ tiền.";
                             }
                             if ($f1 == 1) {
                                 echo "Điểm lô phải chia hết cho 20.";
                             }
                             if ($f1 == 2) {
                                 echo "Dự đoán của bạn đã đóng. Mời dự đoán trận đấu khác.";
                             }
                             if ($f1 == 3) {
                                 echo "Số tiền dự đoán của bạn phải lớn hơn 0.";
                             }
							 $f = false;
                             ?></div>
                <?php } ?>
                <table class="table table-bordered table-events">

                    <thead>
                        <?php if ($__key == "bongda") { ?>
                            <tr class="heading">
                                <th>Trận đấu</th>
                                <th>Chọn</th>
                                <th>Kiểu</th>
                                <th style="width: 63px;">Đặt</th>
                                <th style="width: 63px;">Thắng</th>
                                <th style="width: 85px;"></th>
                            </tr>
                        <?php } else if ($__key == "soxo") { ?>
                            <tr class="heading">
                                <th>Chọn Số</th>
                                <th>Miền</th>
                                <th>Kiểu</th>
                                <th style="width: 63px;">Đặt</th>
                                <th style="width: 63px;">Thắng</th>
                                <th style="width: 85px;"></th>
                            </tr>
                        <?php } else { ?>
                            <tr class="heading">
                            </tr>
                        <?php } ?>
                    </thead>
                    <tbody>
                        <?php
                        if ($__key == "bongda") {
                            foreach ($_SESSION["cart"]["bongda"] as $keys => $_cart) {
                                $base_decode = base64_decode($_cart);
                                $json_decode = json_decode($base_decode);
                                $match = Frontend::findMatchById((int) $json_decode->match_id);
                                foreach ($match as $_match) {
                                    $team1 = Frontend::findTeamById((int) $_match["Team1"]);
                                    $team2 = Frontend::findTeamById((int) $_match["Team2"]);
                                    $odd_value = Frontend::findOddById((int) $json_decode->odd_id);
                                    foreach ($odd_value as $_odd_value) {
                                        $value = explode("|", $_odd_value["value"]);
                                        foreach ($arr_odd_bets as $_arr_odd_bets) {
                                            foreach ($value as $key => $_value) {
                                                if ($_value == $json_decode->odd_value) {
                                                    if (count($_arr_odd_bets[$_odd_value["typeID"]]) == 2) {
                                                        if ($key > 2) {
                                                            if ($key % 2 == 0) {
                                                                $k = 0;
                                                            }
                                                            if ($key % 2 == 1) {
                                                                $k = 1;
                                                            }
                                                        } else {
                                                            $k = $key;
                                                        }
                                                    } else if (count($_arr_odd_bets[$_odd_value["typeID"]]) == 3) {
                                                        if ($key > 3) {
                                                            if ($key % 3 == 0) {
                                                                $k = 0;
                                                            }
                                                            if ($key % 3 == 1) {
                                                                $k = 1;
                                                            }
                                                            if ($key % 3 == 2) {
                                                                $k = 2;
                                                            }
                                                        } else {
                                                            $k = $key;
                                                        }
                                                    } else if (count($_arr_odd_bets[$_odd_value["typeID"]]) == 4) {
                                                        if ($key > 4) {
                                                            if ($key % 4 == 0) {
                                                                $k = 0;
                                                            }
                                                            if ($key % 4 == 1) {
                                                                $k = 1;
                                                            }
                                                            if ($key % 4 == 2) {
                                                                $k = 2;
                                                            }
                                                            if ($key % 4 == 3) {
                                                                $k = 3;
                                                            }
                                                        } else {
                                                            $k = $key;
                                                        }
                                                    }
                                                    $chon_kieu = $_odd_value["title"];
                                                    $chon_cua = $_arr_odd_bets[$_odd_value["typeID"]][$k];
                                                    $odd_id = $_odd_value["_id"];
                                                    $index_key = $key;
                                                }
                                            }
                                        }
                                    }
                                    ?>
                                    <tr class="alert">
                                        <td style="text-align: left;">
                                            <a href="#">
                                                <?php
                                                foreach ($team1 as $_team1) {
                                                    echo $_team1["name"] . " - ";
                                                }
                                                foreach ($team2 as $_team2) {
                                                    echo $_team2["name"];
                                                }
                                                ?></a>
                                        </td>
                                        <td style="text-align: left;"><?php echo $chon_cua ?><span class="label label-warning" style="margin-left: 10px"><?php echo $json_decode->odd_value; ?></span></td>
                                        <td style="text-align: left;"><?php echo $chon_kieu ?><input type="hidden" name="index_key[<?php echo $__key . "_" . $keys ?>]" value="<?php echo $index_key; ?>"/></td>
                                        <td>
                                            <input id="number_money_<?php echo $__key . "_" . $keys ?>" name="number_money[<?php echo $__key . "_" . $keys ?>]" type="text" maxlength="4" value="<?php
                                            if ($json_decode->money)
                                                echo $json_decode->money;
                                            else
                                                echo "0";
                                            ?>" style="width: 50px" onkeydown="checknumber('<?php echo $keys ?>')"/>
                                            <!--<a class="btn bt-log" data-toggle="modal" role="button" href="#login" type="submit">Đăng nhập</a>-->
                                        </td>
                                        <td>
                                            <input id="total_money_<?php echo $__key . "_" . $keys ?>" readonly="" type="text" value="<?php
                                            if ($json_decode->total_money)
                                                echo $json_decode->total_money;
                                            else
                                                echo "0";
                                            ?>" style="width: 65px"/>
                                        </td>
                                        <td>
                                            <a id="btnupdate" onclick="update_datcuoc('<?php echo $__key ?>', '<?php echo $_cart ?>', '<?php echo $keys ?>', '<?php echo $index_key; ?>')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/update.png"/></a>
                                            <a id="btndelete" onclick="delete_datcuoc('<?php echo $__key ?>', '<?php echo $keys ?>')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/delete.png"/></a>
                                        </td>
                                    </tr>
                                <?php } ?>
                            <?php } ?>
                            <?php if (!isset($_SESSION["cart"]["soxo"])) { ?>
                                <tr class="alert">
                                    <td colspan="6" style="text-align: right">
                                        <button type="submit" class="btn btn-inverse btn-large" id="btndatcuoc" style="padding: 7px 9px; font-size: 15px">Đặt cược</button>
                                    </td>
                                </tr>
                            <?php } ?>
                            <?php
                        } else if ($__key == "soxo") {
                            foreach ($_SESSION["cart"]["soxo"] as $keys => $_cart) {
                                $base_decode = base64_decode($_cart);
                                $json_decode = json_decode($base_decode);
                                ?>
                                <tr class="alert">
                                    <td style="text-align: left;">
                                        <?php echo $json_decode->value ?>
                                    </td>
                                    <td style="text-align: left;"><?php
                                        if ($json_decode->location == 1) {
                                            echo "Bắc";
                                        } else {
                                            echo "Nam";
                                        }
                                        ?></td>
                                    <td style="text-align: left;"><?php
                                        if ($json_decode->type == 1) {
                                            echo "Đề";
                                        } else {
                                            echo "Lô";
                                        }
                                        ?></td>
                                    <td>
                                        <input id="number_money_<?php echo $__key . "_" . $keys ?>" name="number_money[<?php echo $__key . "_" . $keys ?>]" type="text" maxlength="4" value="<?php
                                        if ($json_decode->money)
                                            echo $json_decode->money;
                                        else
                                            echo "0";
                                        ?>" style="width: 50px" onkeydown="checknumber('<?php echo $keys ?>')"/>
                                        <!--<a class="btn bt-log" data-toggle="modal" role="button" href="#login" type="submit">Đăng nhập</a>-->
                                    </td>
                                    <td>
                                        <input id="total_money_<?php echo $__key . "_" . $keys ?>" readonly="" type="text" value="<?php
                                        if ($json_decode->total_money)
                                            echo $json_decode->total_money;
                                        else
                                            echo "0";
                                        ?>" style="width: 65px"/>
                                    </td>
                                    <td>
                                        <a id="btnupdate" onclick="update_datcuoc('<?php echo $__key ?>', '<?php echo $_cart ?>', '<?php echo $keys ?>', '')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/update.png"/></a>
                                        <a id="btndelete" onclick="delete_datcuoc('<?php echo $__key ?>', '<?php echo $keys ?>')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/delete.png"/></a>
                                    </td>
                                </tr>
                            <?php } ?>
                            <tr class="alert">
                                <!--<td colspan="5" style="text-align: right"><b>Tổng điểm thắng : </b><input id="total_money" readonly="" type="text" value="" style="width: 65px"/></td>-->
                                <td colspan="6" style="text-align: right">
                                    <button type="submit" class="btn btn-inverse btn-large" id="btndatcuoc" style="padding: 7px 9px; font-size: 15px">Đặt cược</button>
                                </td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
                <?php
            }
        } else {
            ?>
            <table class="table table-bordered table-events">
                <tbody>
                    <tr class="alert">
                        <td><?php
                            if ($flag == 2) {
                                echo "Dự đoán thành công.";
                            } else if ($flag == 1) {
                                echo "Tài khoản bạn không đủ tiền.";
                            } else {
                                echo "Chưa có dự đoán nào.";
                            }
                            ?></td>
                    </tr>
                </tbody>
            </table>        
            <?php
        }
        ?>
    </form>
</div>
<script>
    function checknumber($key) {
        $('#number_money_' + $key).keydown(isNumbericOnly);
    }
</script>